package com.atguigu.gulimall.order.interceptor;

import com.atguigu.common.constant.AuthServerConstat;
import com.atguigu.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginUserInterceptor implements HandlerInterceptor {
    //目标请求到达之前，做一个前置拦截
    public static ThreadLocal<MemberRespVo> LoginUser = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstat.LOGIN_USER);
        if (attribute!=null){
            //登录了 才可以
            LoginUser.set(attribute);
            return true;
        }else {
            //没登录 重定向到登录页
            request.getSession().setAttribute("msg","请先进行登录");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
